package com.zhongwang.business.module.estimateBudgetAccountingManage.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.BusBudgetAccountingCost;
import com.zhongwang.business.gen.entity.BusGeneralHours;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingInfoDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.BusGeneralHoursModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AssemblyAuxExportModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AssemblyExportModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.CutAuxExportModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.CutAuxImportModel;
import com.zhongwang.business.util.ShiroUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 徐一贺
 * @className CutAuxImportListener
 * @description 下料辅材信息导入监听器
 * @date 2020/11/21 14:46
 * @versron ver 1.0
 */
public class AssemblyAuxImportListener extends AnalysisEventListener<AssemblyAuxExportModel> {
    /**
     * 钩子
     */
    private ImportCallBack callBack;
    /**
     * Excel相关参数模型
     */
    private AssemblyExportModel param;
    /**
     * 材料信息数据持久层
     */
    private AccountingInfoDao accountingInfoDao;
    /**
     * 错误列表
     */
    private List<AssemblyAuxExportModel> errorList = new ArrayList<>();
    /**
     * 可用数据列表
     */
    private List<BusGeneralHoursModel> hoursList = new ArrayList<>();
    /**
     * 当前时间
     */
    private Date currentTime = new Date();

    /**
     * 构造函数
     *
     * @param param             页面参数
     * @param callBack          回调函数
     * @param accountingInfoDao 持久层
     */
    public AssemblyAuxImportListener(AssemblyExportModel param, ImportCallBack callBack, AccountingInfoDao accountingInfoDao) {
        this.callBack = callBack;
        this.param = param;
        this.accountingInfoDao = accountingInfoDao;
    }

    /**
     * 验证
     *
     * @param data 每行数据
     */
    @Override
    public void invoke(AssemblyAuxExportModel data, AnalysisContext context) {
        StringBuffer warningMessage = new StringBuffer();
        if (data.getLaborName() != null && !data.getLaborName().equals("")) {
            data.setLaborName(data.getLaborName().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getLaborName() == null) {
            warningMessage.append("请填写主要类别!\n！\r\n");
        } else if ("".equals(data.getLaborName())) {
            warningMessage.append("请填写主要类别!\n！\r\n");
        }
        if (data.getLaborNum() != null && !data.getLaborNum().equals("")) {
            data.setLaborNum(data.getLaborNum().replace("\r", "").replace("\n", "").trim());
        }
        if (data.getLaborNum() == null) {
            warningMessage.append("请填写主要辅材用量！\r\n");
        } else if ("".equals(data.getLaborNum())) {
            warningMessage.append("请填写主要辅材用量！\r\n");
        } else {
            try {
                BigDecimal num = new BigDecimal(data.getLaborNum());
                //小于等于-1
                if (num.compareTo(new BigDecimal(-1)) < 1) {
                    warningMessage.append("主要辅材用量不正确！请修改正确后重新导入！正确的主要辅材用量应该是非负数字。\r\n");
                }
            } catch (Exception e) {
                warningMessage.append("主要辅材用量不正确！请修改正确后重新导入！正确的主要辅材用量应该是非负数字。\r\n");
            }
        }
        if (data.getUnit() != null && !data.getUnit().equals("")) {
            data.setUnit(data.getUnit().replace("\r", "").replace("\n", "").trim());
        }if (data.getUnit() == null) {
            warningMessage.append("请填写单位！\r\n");
        } else if ("".equals(data.getUnit())) {
            warningMessage.append("请填写单位！\r\n");
        }
        if (data.getPrice() == null) {
            warningMessage.append("请填写单价！\r\n");
        } else if ("".equals(data.getPrice())) {
            warningMessage.append("请填写单价！\r\n");
        } else {
            try {
                BigDecimal price = new BigDecimal(data.getPrice());
                //小于等于-1
                if (price.compareTo(new BigDecimal(-1)) < 1) {
                    warningMessage.append("单价不正确！请修改正确后重新导入！正确的单价应该是非负数字。\r\n");
                }
            } catch (Exception e) {
                warningMessage.append("单价不正确！请修改正确后重新导入！正确的单价应该是非负数字。\r\n");
            }
        }
        if (warningMessage == null || warningMessage.toString().length() == 0) {
            BusBudgetAccountingCost cost = accountingInfoDao.getBusBudgetAccountingCostByPrimaryKey(param.getId());
            data.setBatId(param.getBatId());
            data.setMajor(param.getMajor());
            data.setPart(param.getPart());
            data.setDataUseType(3);
            data.setProCode(param.getProCode());
            data.setGetDate(currentTime);
            data.setGetUid(ShiroUtils.getUserId());
            data.setPriceType(2);
            data.setCostId(cost.getId());
            data.setMajor(cost.getMajor());
            data.setPart(cost.getPart());
            data.setIsEffectiveFlag(1);
            data.setIsPrototypeDataFlag(0);
            if (param.getMajor().equals("机械")) {
                data.setType(31);
            } else if (param.getMajor().equals("流体")) {
                data.setType(32);
            } else if (param.getMajor().equals("自动化")) {
                data.setType(33);
            }
            data.setLaborType(14);
            hoursList.add(modeConvert(data));
        } else {
            data.setWarnInfo(warningMessage.toString());
            errorList.add(data);
        }
    }

    /**
     * 持久化
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        BusBudgetAccountingCost cost = accountingInfoDao.getBusBudgetAccountingCostByPrimaryKey(param.getId());
        if (errorList.size() > 0) {
            callBack.onWarning(errorList);
        } else if (hoursList.size() > 0) {
            //对数据进行无效话操作
            BusGeneralHoursModel model = new BusGeneralHoursModel();
            if (param.getMajor().equals("机械")) {
                model.setTypes("31");
            } else if (param.getMajor().equals("流体")) {
                model.setTypes("32");
            } else if (param.getMajor().equals("自动化")) {
                model.setTypes("33");
            }
            model.setLaborTypes("14");
            model.setCostId(cost.getId());
            model.setIsEffectiveFlag(2);
            accountingInfoDao.updateBusGeneralHoursIsEffectiveFlagByCostId(model);
            for (int i = 0; i < hoursList.size(); i++) {
                accountingInfoDao.insertBusGeneralHours(hoursList.get(i));
            }
            cost.setDataSource("1");
            accountingInfoDao.updateBusBudgetAccountingCostDataSourceByPrimaryKey(cost);
            callBack.onSuccess();
        }
    }

    /**
     * 异常处理
     *
     * @param exception 异常信息
     * @param context
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exception.printStackTrace();
    }

    /**
     * rawMatModelToBusMaterialData 模型转换类
     *
     * @param
     * @return com.zhongwang.business.gen.entity.BusMaterialData
     * @throws
     * @author 徐一贺
     * @date 2020/11/21 15:09
     */
    private BusGeneralHoursModel modeConvert(AssemblyAuxExportModel source) {
        BusGeneralHoursModel cutExportModel = new BusGeneralHoursModel();
        cutExportModel.setId(source.getId());
        cutExportModel.setProCode(source.getProCode());
        cutExportModel.setPart(source.getPart());
        cutExportModel.setMajor(source.getMajor());
        cutExportModel.setLaborName(source.getLaborName());
        cutExportModel.setLaborNum(new BigDecimal(source.getLaborNum()));
        cutExportModel.setUnit(source.getUnit());
        cutExportModel.setRemark(source.getRemark());
        cutExportModel.setPrice(new BigDecimal(source.getPrice()));
        cutExportModel.setType(source.getType());
        cutExportModel.setLaborType(source.getLaborType());
        cutExportModel.setBatId(source.getBatId());
        cutExportModel.setPlaceDelivery(source.getPlaceDelivery());
        cutExportModel.setDestination(source.getDestination());
        cutExportModel.setCarNum(source.getCarNum());
        cutExportModel.setDistance(source.getDistance());
        cutExportModel.setDistanceUnit(source.getDistanceUnit());
        cutExportModel.setDsId(source.getDsId());
        cutExportModel.setPriceType(source.getPriceType());
        cutExportModel.setDataUseType(source.getDataUseType());
        cutExportModel.setSourceProCode(source.getSourceProCode());
        cutExportModel.setSourcePart(source.getSourcePart());
        cutExportModel.setGetDate(source.getGetDate());
        cutExportModel.setAmount(source.getAmount());
        cutExportModel.setGetUid(source.getGetUid());
        cutExportModel.setMaterial(source.getMaterial());
        cutExportModel.setPostName(source.getPostName());
        cutExportModel.setPostTel(source.getPostTel());
        cutExportModel.setPostDept(source.getPostDept());
        cutExportModel.setIsEffectiveFlag(source.getIsEffectiveFlag());
        cutExportModel.setCostId(source.getCostId());
        cutExportModel.setIsPrototypeDataFlag(source.getIsPrototypeDataFlag());
        return cutExportModel;
    }
}
